﻿using ConfExpert.Forms.Base;
using DevExpress.XtraEditors.Repository;
using DevTeam;
using System;
using System.Data;

namespace ConfExpert.Forms.Sistem
{
    public partial class IzinYonetimi : XForm
    {
        public IzinYonetimi()
        {
            InitializeComponent();
        }

        private void IzinYonetimi_Load(object sender, EventArgs e)
        {

            gridView1.Init("GetPDKSIzin2", 0);
            gridView1.AddSummary();

            /*
            gridView1.AddDetailGrid(gridView2, "KartNo");
            gridView2.OptionsView.NewItemRowPosition = DevExpress.XtraGrid.Views.Grid.NewItemRowPosition.Bottom;
            //gridView2.Columns["Tur"].ColumnEdit = riAksiyon;
            gridView2.AddColumn("Iz", typeof(DateTime), "ISNULL(IzinBitis, IzinBaslangic)");
            gridView2.MakeColumnInvisible("Iz");

            gridView1.AddColumn("SonIzin", typeof(DateTime), "MAX(Child({FK}).Iz)");
            gridView1.FormatAsDate("SonIzin");
            gridView1.AddSummary();
            gridView2.MakeColumnInvisible("PDKSIzinId");
            gridView2.SetCaptions("IzinBaslangic:Başl. Gün", "IzinBitis:Bitiş Gün", "IzinBaslangicSaat:Başl. Saat", "IzinBitisSaat:Bitiş Saat");
            gridView2.FormatAsDate("IzinBaslangic", "IzinBitis");

            //RepositoryItemTimeSpanEdit ri2x = new RepositoryItemTimeSpanEdit();
            //RepositoryItemTimeEdit ri2x = new RepositoryItemTimeEdit();
            //ri2x.Mask.EditMask = "HH:mm";
            //ri2x.Mask.UseMaskAsDisplayFormat = true;
            //ri2x.AllowNullInput = DevExpress.Utils.DefaultBoolean.True;
            //ri2x.ExportMode = DevExpress.XtraEditors.Repository.ExportMode.DisplayText;

            //foreach (string col in "IzinBaslangicSaat IzinBitisSaat".Split(" ".ToCharArray()))
            //    gridView2.Columns[col].ColumnEdit = ri2x;

            gridView1.BestFitColumns();
            gridView2.BestFitColumns();

            splitContainerControl1.AutoSize();
             */

            splitContainerControl1.PanelVisibility = DevExpress.XtraEditors.SplitPanelVisibility.Panel1;
            btnKaydet.ItemClick += btnKaydet_ItemClick;

            gridView1.FocusedRowChanged += GridView1_FocusedRowChanged;

            string col = "Sure2";
            RepositoryItemTextEdit r = new RepositoryItemTextEdit();
            r.ExportMode = DevExpress.XtraEditors.Repository.ExportMode.DisplayText;
            gridView1.Columns[col].ColumnEdit = r;
            gridView1.CustomColumnDisplayText += gridView1_CustomColumnDisplayText;

            gridView1.BestFitColumns();
        }

        void gridView1_CustomColumnDisplayText(object sender, DevExpress.XtraGrid.Views.Base.CustomColumnDisplayTextEventArgs e)
        {
            if (e.ListSourceRowIndex > -1 && e.Column.FieldName == "Sure2")
            {
                int rh = gridView1.GetRowHandle(e.ListSourceRowIndex);
                DataRow dr = gridView1.GetDataRow(rh);

                if (e.Value.ToInt() != 0)
                {
                    decimal val = e.Value.ToDecimal();
                    bool eksi = Math.Sign(val) < 0;
                    val = Math.Abs(val);

                    int mn = (int)((val - Math.Truncate(val)) * 60);
                    int hr = (int)(val % 10);
                    int dy = (int)(val / 10);   //1 mesai günü = 10 saat

                    //                    e.DisplayText = string.Format("{0:#}" + (dy != 0 ? "." : "") + "{1:00}:{2:00}", dy, hr, mn);
                    e.DisplayText = string.Format((eksi ? "-" : "") + "{0:0}.{1:00}:{2:00}", dy, hr, mn);

                }
                else
                    e.DisplayText = string.Empty;
            }
        }

        private void GridView1_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e)
        {
            gridView2.BestFitColumns();
        }

        void btnKaydet_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            gridView1.EndEditing();
            gridView1.Update();
        }
    }
}
